//
// Created by HP on 2024/2/18.
//

#include "LCD_thread.h"
#include "aurora_ui.h"
#include "aurora_wave.h"


ALIGN(RT_ALIGN_SIZE)
char lcd_thread_stack[LCD_THREAD_STACK_SIZE];
struct rt_thread  __attribute__((section(".ccmram"))) lcd_thread;

EncoderKey_Driver encoderkey;
void LCD_thread(void *param){
    ui2foc_msg.cmd = IS_NONE;
    LCD_Driver lcd;            //lcd屏幕驱动
    Eflash_Driver eflash;      //外部flash驱动
    Aurora_Font font(&eflash);  //字体
    Aurora_UI ui;
    lcd.Init(3);
    lcd.fill(0xffff);
    ui.addEncoderKey_Driver(&encoderkey);
    ui.add_Driver(&lcd,&font);

    rt_thread_mdelay(5000);
    font.Load_FOC_Parameter();
    if(ui2foc_msg.foc_param.L_FOC_PARM.storage_flag == PARAM_VALID){
        ui2foc_msg.type = load_focL_parameter;
        ui2foc_msg.cmd  = IS_CMD;
        rt_thread_mdelay(500);
    }
    if(ui2foc_msg.foc_param.R_FOC_PARM.storage_flag== PARAM_VALID){
        ui2foc_msg.type = load_focR_parameter;
        ui2foc_msg.cmd  = IS_CMD;
        rt_thread_mdelay(500);
    }

    ui.load_ui_parameter();
    if( ui.ui_param.font_big_flag != UI_ENABLE || ui.ui_param.font_middle_flag != UI_ENABLE || ui.ui_param.font_small_flag != UI_ENABLE ){
        rt_thread_mdelay(2000);
        usb_printf("please download font file,and then reset \r\n");

        rt_thread_mdelay(25);
        if( ui.ui_param.font_big_flag == UI_ENABLE )
            usb_printf("big font file : OK\r\n");
        else
            usb_printf("big font file : Fail\r\n");
        rt_thread_mdelay(15);
        if( ui.ui_param.font_middle_flag == UI_ENABLE )
            usb_printf("middle font file : OK\r\n");
        else
            usb_printf("middle font file : Fail\r\n");
        rt_thread_mdelay(15);
        if( ui.ui_param.font_small_flag == UI_ENABLE )
            usb_printf("small font file : OK\r\n");
        else
            usb_printf("small font file : Fail\r\n");
    }
    else
      ui.boot_animation();  // 启动开机动画

    while(true){
        if( foc2ui_msg.cmd != IS_NONE){
            ui.dealmsgEvent_from_FOC(&foc2ui_msg);
            foc2ui_msg.cmd = IS_NONE;
        }
        if( comm2ui_msg.cmd != IS_NONE) {
            ui.dealmsgEvent_from_COMM(&comm2ui_msg);
            comm2ui_msg.cmd = IS_NONE;
        }
        ui.dealKeyEvent();            //处理按键事务
        rt_thread_mdelay(5);
    }
}


//GPIO 外部中断
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
    //编码按键按下
    if( GPIO_Pin == EC_KEY_Pin || GPIO_Pin == EC_AR_Pin){
        encoderkey.EncoderKey_EXTI_Callback(GPIO_Pin);
    }
}